<?php

namespace App\Exports\Cruise;

use App\Models\Cruise\CruiseCheckCompanyModel;
use App\Models\Cruise\CruiseCheckTaskGroupModel;
use App\Models\Cruise\CruiseCheckTaskModel;
use App\Remote\Rms\RmsShopRemote;
use App\Remote\Rms\RmsUserRemote;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;

class CruiseCheck5Export implements FromArray
{
    public Collection $originalData;
    public Collection $cruiseCheckCompanyCollectionGroup;
    public array      $excelHeaders;
    public array      $excelContents = [];

    public function __construct(Collection $originalData)
    {
        $this->originalData         = $originalData;
        $CruiseCheckCompanyIdsGroup = [];
        $this->originalData->each(function (CruiseCheckTaskModel $cruiseCheckTaskModel) use (
            &
            $CruiseCheckCompanyIdsGroup
        ) {
            $cruiseCheckTaskModel->cruiseTaskGroupsRel->each(function (
                CruiseCheckTaskGroupModel $cruiseCheckTaskGroupModel
            ) use (&$CruiseCheckCompanyIdsGroup) {
                $CruiseCheckCompanyIdsGroup = array_merge(
                    $CruiseCheckCompanyIdsGroup,
                    json_decode(attributeGet($cruiseCheckTaskGroupModel, "beCruiseCheckCompanyIds", "[]"), true),
                    []
                );
            });
        });

        $cruiseCheckCompanyIdsGroup              = array_filter(array_unique($CruiseCheckCompanyIdsGroup));
        $cruiseCheckCompanyModelGroup            = CruiseCheckCompanyModel::query()
                                                                          ->with(["fsCompanyRel","fsCompanyUserRel"])
                                                                          ->whereIn("id", $cruiseCheckCompanyIdsGroup)
                                                                          ->get();

        $this->cruiseCheckCompanyCollectionGroup = $cruiseCheckCompanyModelGroup->keyBy("id");


    }

    public function array(): array
    {
        // TODO: Implement array() method.
        $this->generateExcelHeader();
        $this->originalData->map(function (CruiseCheckTaskModel $cruiseCheckTaskModel) {
            $cruiseCheckTaskModel
                ->cruiseTaskGroupsRel
                ->each(function (CruiseCheckTaskGroupModel $cruiseCheckTaskGroupModel) use ($cruiseCheckTaskModel) {
                    collect(json_decode(attributeGet($cruiseCheckTaskGroupModel, "beCruiseCheckCompanyIds", "[]")))
                        ->each(function ($cruiseCheckCompanyId) use (
                            $cruiseCheckTaskGroupModel,
                            $cruiseCheckTaskModel
                        ) {
                            array_push($this->excelContents, [
                                date("m月", attributeGet($cruiseCheckTaskGroupModel, "createdAt.timestamp", 0)),
                                $cruiseCheckTaskModel->name,
                                $cruiseCheckTaskGroupModel->role == CruiseCheckTaskGroupModel::ROLE_USER ?
                                    "内部员工" :
                                    attributeGet($cruiseCheckTaskGroupModel, "fsCompanyRel.name", "-"),
                                attributeGet($this->cruiseCheckCompanyCollectionGroup,
                                    "{$cruiseCheckCompanyId}.fsCompanyRel.name", "-"),
                                attributeGet($this->cruiseCheckCompanyCollectionGroup,
                                    "{$cruiseCheckCompanyId}.fsCompanyUserRel.name", "-"),
                                $cruiseCheckTaskModel->statusText
                            ]);
                        });
                });

        })->values()->toArray();

//        echo collect(
//            [$this->excelHeaders]
//        )->merge($this->excelContents)->toJson(JSON_UNESCAPED_UNICODE);die;
        return collect(
            [$this->excelHeaders]
        )->merge($this->excelContents)->toArray();
    }


    public function generateExcelHeader()
    {
        $this->excelHeaders = [
            "月份",
            "任务名称",
            "稽核方",
            "工厂名称",
            "工厂联系人",
            "任务状态"
        ];
    }
}
